home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-06-20 | 7.1 KB | 290 lines | [TEXT/MPS ] |
- #include <types.h>
- #include <Quickdraw.h>
- //#include <windows.h>
- #include <memory.h>
- //#include <controls.h>
- //#include <Dialogs.h>
- #include <ToolUtils.h>
- #include <Packages.h>
- #include <String.h>
- #include <Strings.h>
- //#include <Errors.h>
- #include <Resources.h>
- #include <files.h>
-
- #include "THyperXCmd.h"
-
- Handle BuildPathname (TXCMDBlock *myParamPtr, StringPtr fName, short vRefNum);
-
- pascal void getWorks ( TXCMDBlock *myParamPtr )
- {
-
- SFTypeList typeList;
- SFReply reply;
- Point where;
- OSErr theErr;
- long fileSize;
- Handle fileNameHandle=nil;
- long theOffset;
-
- const OSType AWDBtype=0x41574442;
- const Size maxBlank=10;
-
-
- /*
- check for the correct number of parameters
- */
-
- if ( !myParamPtr->informArgNums((char *) "getWorks", 3) )
- {
- return;
- }
-
- /*
- Set up the parameter block. If the parameter block can be recovered from
- the global myParamBlock, use it, otherwise, set up a new one and get the
- file.
- */
-
- ParmBlkPtr myParamBlock= (ParmBlkPtr) myParamPtr->RecoverLongGlobal((StringPtr) "\pmyParamBlock");
- if (!myParamBlock)
- {
- myParamBlock=(ParmBlkPtr)NewPtr(sizeof(FileParam));
- if (!myParamBlock)
- {
- myParamPtr->SignalFatalError((char *) "\pNot enough memory for file operations.");
- return;
- }
-
- myParamBlock->ioParam.ioCompletion=0;
-
- /*
- Get the filename from the specified field. The value passed is the id of
- a card field which contains the full pathname to the desired file.
- If a file is found with that name, we use that file, otherwise, return the
- error as the XFCN result. If the field is empty, we call SFGet to get a
- filename and recover the full pathname to the file, which we place in
- the specified background field.
- */
-
- short listFieldID= (short) myParamPtr->RecoverLongArg(0);
- fileNameHandle=myParamPtr->GetFieldByID(true, listFieldID);
- HLock(fileNameHandle);
-
- if (strlen(*fileNameHandle))
- {
- c2pstr(*fileNameHandle);
- myParamBlock->fileParam.ioNamePtr=(StringPtr)*fileNameHandle;
- myParamBlock->fileParam.ioVRefNum=0;
- myParamBlock->fileParam.ioFRefNum=0;
- myParamBlock->fileParam.ioFDirIndex=0;
-
- if (theErr=PBGetFInfo(myParamBlock, false))
- {
- myParamPtr->SignalFileErr(theErr);
- return;
- }
- else if ((myParamBlock->fileParam.ioFlFndrInfo.fdType)!=AWDBtype)
- {
- myParamPtr->SignalFatalError((char *) "\pFile not Works database!");
- return;
- }
- else
- {
- fileSize=myParamBlock->fileParam.ioFlLgLen;
- }
- DisposHandle(fileNameHandle);
-
- }
- else
- {
- typeList[0]=AWDBtype;
- where.v=50;
- where.h=50;
- SFGetFile(where, "\pSelect file to load", nil, 1, typeList, nil, &reply);
- if (reply.good)
- {
- myParamBlock->ioParam.ioVRefNum=reply.vRefNum;
- myParamBlock->ioParam.ioNamePtr=(StringPtr)&(reply.fName);
- Handle fsHandle=BuildPathname (myParamPtr, reply.fName, reply.vRefNum);
- if (fsHandle)
- myParamPtr->SetFieldByID(true, listFieldID, fsHandle);
- else
- return;
- c2pstr((char *)&(reply.fName));
- }
- else
- {
- myParamPtr->SignalFatalError( (char *) "\pFile not specified.");
- return;
- }
- }
-
- /*
- Open the file for readOnly access. If we got the file name from SFFile,
- get the file size (We already got the file size for the explicitly named
- file with the call to PBGetFInfo)
- */
-
- myParamBlock->ioParam.ioVersNum=0;
- myParamBlock->ioParam.ioPermssn=fsRdPerm;
- myParamBlock->ioParam.ioMisc=0;
- if (theErr=PBOpen(myParamBlock, false))
- {
- myParamPtr->SignalFileErr(theErr);
- return;
- }
- if (!fileSize)
- {
- if (theErr=PBGetEOF(myParamBlock, false))
- {
- myParamPtr->SignalFileErr(theErr);
- return;
- }
- fileSize=(long)myParamBlock->ioParam.ioMisc;
- }
- myParamPtr->SetLongGlobal((StringPtr) "\pmyFileSize", fileSize);
- myParamPtr->SetLongGlobal((StringPtr) "\pmyParamBlock", (long) myParamBlock);
-
- }
-
-
- // Position the file to the start of the database data and read the length word
-
- fileSize = myParamPtr->RecoverLongGlobal((StringPtr) "\pmyFileSize");
- theOffset= myParamPtr->RecoverLongGlobal((StringPtr) "\pmyOffset");
-
- if (!theOffset)
- theOffset=0x058F;
- short theLength=0;
- long theCount=2;
- myParamBlock->ioParam.ioBuffer=(Ptr)&theLength;
- myParamBlock->ioParam.ioReqCount=theCount;
- myParamBlock->ioParam.ioPosMode=fsFromStart;
- myParamBlock->ioParam.ioPosOffset=theOffset;
- if (theErr=PBRead(myParamBlock, false))
- {
- myParamPtr->SignalFileErr(theErr);
- return;
- }
-
- theOffset+=theCount;
-
- if (theLength==0x03F6)
- {
- do
- {
- theOffset+=theLength;
- long theCount=2;
- myParamBlock->ioParam.ioBuffer=(Ptr)&theLength;
- myParamBlock->ioParam.ioReqCount=theCount;
- myParamBlock->ioParam.ioPosMode=fsFromStart;
- myParamBlock->ioParam.ioPosOffset=theOffset;
- if (theErr=PBRead(myParamBlock, false))
- {
- myParamPtr->SignalFileErr(theErr);
- return;
- }
- theOffset+=theCount;
- }
- while (theLength==0x03F6);
- }
-
- short outField= (short) myParamPtr->RecoverLongArg(2);
- Handle stringHandle=NewHandle((Size) theLength + maxBlank);
- if (theErr=MemError())
- {
- CString ErrorString = CString("Memory error: ") + CString((long)theErr);
- myParamPtr->SignalFatalError(ErrorString);
- return;
- }
- HLock(stringHandle);
- myParamBlock->ioParam.ioBuffer=*stringHandle;
- myParamBlock->ioParam.ioReqCount=theLength-1;
- myParamBlock->ioParam.ioPosMode=fsFromStart;
- myParamBlock->ioParam.ioPosOffset=theOffset;
- if (theErr=PBRead(myParamBlock, false))
- {
- myParamPtr->SignalFileErr(theErr);
- return;
- }
-
- theOffset+=theLength;
- myParamPtr->SetLongGlobal((StringPtr) "\pmyOffset", theOffset);
-
- #ifdef debug
- CString tempString = CString(";dm #") + CString((long) *stringHandle);
- debugstr ((char *) tempString);
- #endif
-
- register Ptr myStringPtr = StripAddress(*stringHandle);
- size_t theLen;
- long cumLen=0;
-
- short numBlank;
- do
- {
- p2cstr((StringPtr)myStringPtr);
- theLen=strlen(myStringPtr);
- myStringPtr[theLen]=(char)0x0D;
- myStringPtr+=theLen+1;
- if ( *myStringPtr == (char)0xFE )
- {
- *(myStringPtr++) = (char)0x0D;
- cumLen++;
- numBlank=*(myStringPtr);
- switch (numBlank)
- {
- case 1:
- BlockMove(myStringPtr+1,myStringPtr,theLength-cumLen-1);
- theLength--;
- cumLen+=1;
- break;
- case 2:
- *(myStringPtr++) = (char)0x0D;
- cumLen+=2;
- break;
- default:
- BlockMove(myStringPtr+1,myStringPtr+numBlank,theLength-cumLen-1);
- numBlank--;
- theLength+=numBlank-1;
- cumLen+=numBlank+1;
- for (short k=0;k<numBlank;k++)
- *(myStringPtr++) = (char)0x0D;
- myStringPtr++;
- break;
- }
- }
- cumLen+=theLen+1;
- }
- while (cumLen<theLength-1);
- *(myStringPtr-1)=(char)0x00;
-
- HUnlock(stringHandle);
- myParamPtr->SetFieldByID(true, outField, stringHandle);
-
- /*
- If the second argument is true, or we are on the last record, then close the file,
- otherwise, do not
- */
- if ( (myParamPtr->RecoverBooleanArg(1))
- || (theOffset>=fileSize-1) )
- {
- if (theErr=PBClose(myParamBlock, false))
- {
- myParamPtr->SignalFileErr(theErr);
- return;
- }
- if (myParamBlock)
- DisposPtr((Ptr)myParamBlock);
- myParamPtr->SetLongGlobal((StringPtr) "\pmyParamBlock", (long) 0);
- myParamPtr->SetLongGlobal((StringPtr) "\pmyOffset", (long) 0);
- myParamPtr->SignalReturnStatus((StringPtr)"\pEOF");
- }
- else
- myParamPtr->SignalReturnStatus((StringPtr)"\pOK");
-
- return;
- }
-
-